<template>
  <el-dialog :title="formData.id?'编辑分类':'新增分类'" :visible="showDialog" @close="closeFn">
    <!-- 表单 -->
    <el-form ref="form" label-width="120px" :rules="rules" :model="formData">
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" style="width:50%" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" style="width:50%" placeholder="请输入描述" />
      </el-form-item>
      <el-form-item v-model="formData.image" label="封面" prop="image">
        <uploadpictures ref="uploadpictures" :staff-photo.sync="formData.image" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="closeFn">取消</el-button>
          <el-button type="primary" size="small" @click="confirmFn">提交</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import uploadpictures from '@/views/fenlei/uploadPictures.vue'
import { addClassify } from '@/api/category'
export default {
  components: { uploadpictures },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
    //   formData: {
    //     title: '',
    //     desc: '',
    //     image: ''
    //   },
      rules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        desc: [{ required: true, message: '描述不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    uploadImgView(form) {
      this.$refs.uploadpictures.fileList.push({
        url: form.image
      })
    },
    closeFn() {
      this.$emit('update:showDialog', false)
      this.$refs.uploadpictures.fileList = []
      this.$refs.form.resetFields()
    },
    async confirmFn() {
      try {
        await this.$refs.form.validate()
        const res = await addClassify({ ...this.formData, image: this.formData.image })
        console.log(59, res)
        this.closeFn()
        this.$emit('updateEvent')
        this.$message.success('新增成功！')
        this.formData = {
          title: '',
          desc: '',
          image: ''
        }
      } catch (error) {
        console.log(error)
      }
    }
  }
}

</script>

<style>

</style>
